package com.msda.utils.strategy;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.msda.pojo.dto.staff.UserDTO;
import com.msda.pojo.entity.staff.Customer;
import com.msda.pojo.entity.staff.CustomerDetail;
import com.msda.pojo.entity.staff.User;
import com.msda.exception.database.DatabaseOperationException;
import com.msda.utils.ImageCompressor;
import com.msda.utils.SnowFlakeID;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static com.msda.constant.DatabaseConstant.INSERT_DEFAULT;

@Transactional
public enum UserStrategy {

    Staff{
        @Override
        public void register(UserDTO userDTO,UserContext context) throws IOException {
            User user = new User();
            BeanUtil.copyProperties(userDTO,user);
            int i = context.getUserMapper().insert(user);
            if(i < 1){
                throw new  DatabaseOperationException(INSERT_DEFAULT);
            }
        }

        @Override
        public Object getUser(User user, UserContext context) {
            return user;
        }

        @Override
        public Object updateUser(UserDTO userDTO, UserContext context) {
            User user = new User();
            BeanUtil.copyProperties(userDTO,user);
            context.getUserMapper().updateById(user);
            User aimUser = context.getUserMapper().selectById(user.getId());
            aimUser.setPassword(null);
            return aimUser;
        }
    },
    Administrator{
        @Override
        public void register(UserDTO userDTO,UserContext context) throws IOException {
             Staff.register(userDTO,context);
        }

        @Override
        public Object getUser(User user, UserContext context) {
            return user;
        }

        @Override
        public Object updateUser(UserDTO userDTO, UserContext context) {
            return Staff.updateUser(userDTO,context);
        }
    },
    Customer{
        @Override
        public void register(UserDTO userDTO,UserContext context) throws IOException {
            User user = new User();
            CustomerDetail customerDetail = new CustomerDetail();
            BeanUtil.copyProperties(userDTO,user);
            BeanUtil.copyProperties(userDTO,customerDetail);
            customerDetail.setId(SnowFlakeID.getSnowflakeId());
            customerDetail.setUserId(user.getId());
            int i = context.getUserMapper().insert(user);
            i += context.getCustomerMapper().insert(customerDetail);
            if(i < 2){
                throw new  DatabaseOperationException(INSERT_DEFAULT);
            }
        }

        @Override
        public Object getUser(User user, UserContext context) {
            com.msda.pojo.entity.staff.Customer customer = new Customer();
            BeanUtil.copyProperties(user,customer);
            QueryWrapper<CustomerDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", user.getId());
            CustomerDetail customerDetail =  context.getCustomerMapper().selectOne(queryWrapper);
            customer.setPhone(customerDetail.getPhone());
            customer.setCompanyName(customerDetail.getCompanyName());
            customer.setPassword(null);
            return customer;
        }

        @Override
        public Object updateUser(UserDTO userDTO, UserContext context) {
            User user = new User();
            BeanUtil.copyProperties(userDTO,user);
            context.getUserMapper().updateById(user);
            QueryWrapper<CustomerDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", user.getId());
            CustomerDetail customerDetail =  context.getCustomerMapper().selectOne(queryWrapper);
            customerDetail.setPhone(userDTO.getPhone());
            customerDetail.setCompanyName(userDTO.getCompanyName());
            context.getCustomerMapper().updateById(customerDetail);

            return getUser(user,context);
        }
    };





    public abstract void register(UserDTO userDTO,UserContext context) throws IOException;

    public abstract Object getUser(User user,UserContext context);

    public abstract Object updateUser(UserDTO userDTO,UserContext context);
}
